package com.xiaosheng.ljswl.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.xiaosheng.ljswl.Config.SerialApplication;
import com.xiaosheng.ljswl.R;
import com.xiaosheng.ljswl.model.Node.*;

import node.utils.RouterUtil;
import node.utils.Sensor;
import serial.utils.SerialZbThread;


public class RelayFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private RouterUtil routerUtil;
    private TextView titleGuangxian;
    private TextView textView2;
    private TextView MacAdd;
    private TextView Rfid;
    private boolean flag;

    public RelayFragment() {
    }

    public static RelayFragment newInstance(String param1, String param2) {
        RelayFragment fragment = new RelayFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_relay, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

        //初始化串口线程

        titleGuangxian = (TextView) view.findViewById(R.id.title_mac);
        textView2 = (TextView) view.findViewById(R.id.textView2);
        MacAdd = (TextView) view.findViewById(R.id.MacAdd);
        Rfid = (TextView) view.findViewById(R.id.Rfid);

        //创建继电器工具类
        routerUtil = new RouterUtil();
        SerialZbThread serialZbThread = ((SerialApplication) getActivity().getApplication()).getSerialZbThread();
        serialZbThread.setiZgibeeRec((buffer, len) -> {
            Router router;
            RFID rfid;
            if ((router = Sensor.getRouter(buffer, len)) != null) {
                this.routerUtil.setMacaddr(router.macaddr);
                this.routerUtil.setAddr(router.addr);
                final Router finalRouter = router;
                getActivity().runOnUiThread(() -> MacAdd.setText(finalRouter.mac));
            } else if ((rfid = Sensor.getRFID(buffer, len)) != null) {
                getActivity().runOnUiThread(() -> {
                    Rfid.setText(rfid.CatId);
                    flag = !flag;
                    if (flag) {
                        serialZbThread.sendZigBee(this.routerUtil.RelayOpen());
                        Toast.makeText(getActivity(), "继电器已开启", Toast.LENGTH_SHORT).show();
                    } else {
                        serialZbThread.sendZigBee(this.routerUtil.RelayClear());
                        Toast.makeText(getActivity(), "继电器已关闭", Toast.LENGTH_SHORT).show();
                    }
                });
            } else {
                serialZbThread.resetComDataHandler();
            }
        });
        super.onViewCreated(view, savedInstanceState);
    }
}